package xyz.sunne.service;

import cn.licoy.encryptbody.util.MD5EncryptUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.sunne.entity.SysRole;
import xyz.sunne.entity.SysUser;
import xyz.sunne.exception.RequestException;
import xyz.sunne.mapper.SysUserMapper;

/**
 * @author Sunne
 * @version 1.0
 * @date 2021/9/27 0027
 */
@Service
public class SysUserService extends BaseService<SysUserMapper, SysUser>{

    @Transactional
    public void addUser(SysUser sysUser){
        SysUser user = this.getByKey(SysUser::getAccount,sysUser.getAccount());
        if (user != null) throw RequestException.fail("用户已经存在");
        //判断角色

        SysUser newuser = new SysUser();
        //拷贝对象
        BeanUtils.copyProperties(sysUser, newuser);

        newuser.setPasswd(MD5EncryptUtil.encrypt(sysUser.getPasswd()));

        if (!this.save(sysUser)){
            throw RequestException.fail("数据保存失败");
        }


    }
}
